package cn.madis.controller;

import cn.madis.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;
    // 熔断注解
    @HystrixCommand(fallbackMethod = "getUserByIdFallbackMethod")
    @GetMapping("/order/{id}")
    public User getUserById(@PathVariable("id") Long id){

        String url ="http://user-server/user/" + id;
        return restTemplate.getForObject(url,User.class);
    }
  // 熔断降级方法  参数和返回值必须和被熔断的方法一致 ，方法名要和  fallbackMethod 的值一致
   public User getUserByIdFallbackMethod(@PathVariable("id") Long id){
        //返回托底数据
        return User.builder().id(-1L).intro("系统繁忙").build();
    }
}
